package servlet;

import javax.servlet.* ;

import javabean.Factory;
import javabean.user.*;

public class RegisterServlet extends BaseServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = -8538076142868886263L;
	
	public void service() throws Exception{
		int tag = 0 ;
		//获取表单提交的值
		String uid = request.getParameter("uid") ;
		//String test = request.getParameter("test");
		//JSONArray json = JSONArray.fromObject(test);
		if(uid!=null && !"".equals(uid)){
			String email = request.getParameter("email");
			String password = request.getParameter("password-md5") ;
			
			if(!"".equals(uid) && !"".equals(email) && !"".equals(password)){
				//将提交的值赋给user对象
				User user = new User() ;
				user.setUid(uid) ;
				user.setPassword(password) ;
				user.setEmail(email) ;
				user.setPic("user-normal.jpg");
				try{
					tag = Factory.getUserDAOInstance().register(user) ;
					getSession().setAttribute("uid", user.getUid()) ;
					getSession().setAttribute("upic","user-normal.jpg");
				}
				catch(Exception e){
					response.getWriter().write("{\"success\":false,\"msg\":\"注册失败，服务器出错\"}");
					System.out.println("服务器或数据库出错");
				}
			}
			if(tag==2){
				response.getWriter().write("{\"success\":true,\"msg\":\"注册成功\"}");
			}else if(tag==1){
				response.getWriter().write("{\"success\":false,\"msg\":\"注册失败\"}");
			}
		}else{
			//跳转
			RequestDispatcher dispatcher = request.getRequestDispatcher("public/register.jsp");
			dispatcher.forward(request, response) ;
		}
	}
}
